package com.ybwh.springboot1;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import com.ybwh.springboot1.dao.ReportMapper;
import com.ybwh.springboot1.model.po.Report;
import io.shardingsphere.transaction.annotation.ShardingTransactionType;
import io.shardingsphere.transaction.api.TransactionType;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;


/**
 * 按月分表例子(自定义分表分库策略)
 *
 */
@RunWith(SpringRunner.class)
@SpringBootTest
// @TestPropertySource(properties=
// {"spring.config.location=E:/application.yml"})
public class TesShardingTableByMonth {

	@Autowired
	private ReportMapper dao;


//	@ShardingTransactionType(TransactionType.LOCAL)
//	@ShardingTransactionType(TransactionType.XA)
//	@Transactional
	@Test
	public void testInsert() throws ParseException {
		Assert.assertNotNull(dao);

		
		DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		Report report = new Report();
		report.setCreateTime(dFormat.parse("2018-09-11 16:27:20"));
		report.setSaleCount(999);
		report.setSaleAmount(new BigDecimal(99999999.44D));

		try {
			dao.insertSelective(report);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void testSelect() {
		Assert.assertNotNull(dao);

		try {

			Report report = dao.selectByPrimaryKey(248505409994752004L);
			System.out.println(report);


//			DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//
//			List<Report> list = dao.selectEqCreateTime(dFormat.parse("2018-09-11 16:27:20"));
//			System.out.println(list);

//			List<Report> list1 = dao.selectInCreateTime(Arrays.asList(dFormat.parse("2018-09-17 16:02:01")));
//			System.out.println(list1);
			
//			List<Report> list2 = dao.selectBetweenCreateTime(dFormat.parse("2018-08-17 16:02:00"), dFormat.parse("2018-09-17 16:27:20"));
//			System.out.println(list2);
			
			
//			Integer count = dao.selectCount(dFormat.parse("2018-08-16 16:02:00"), dFormat.parse("2018-09-17 16:27:20"));
//			System.out.println(count);
//
//			long sum = dao.selectSum(dFormat.parse("2018-08-17 16:02:00"), dFormat.parse("2018-09-17 16:27:20"));
//			System.out.println(sum);
			
			
			System.out.println(
					"----------------------------------------------------------------------------------------------");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
